package com.jinlia.show.modules.system.controller.sys;

import com.jinlia.show.modules.base.system.entity.SysAreaCode;
import com.jinlia.show.modules.base.system.service.AreaCodeMapperService;
import com.jinlia.show.modules.system.model.vo.AreaCodeVO;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@RestController
@RequiredArgsConstructor
@RequestMapping("/area/code")
public class SysAreaCodeController {
    final AreaCodeMapperService areaCodeMapperService;

    @PostMapping(value = "/get")
    public ResponseEntity<AreaCodeVO> getListVO(@RequestBody SysAreaCode areaCode) {
        AreaCodeVO returnVO = new AreaCodeVO();
        List<SysAreaCode> list= areaCodeMapperService.getList(areaCode);
        if (Objects.isNull(areaCode.getCode())) {//查询省份
            returnVO.setCodeType("province");
        } else if (areaCode.getCode() % 10000 == 0) { //查询城市
            returnVO.setCodeType("city");
        } else {//查询地区
            returnVO.setCodeType("area");
        }
        //去除自己
        list = list.stream().filter(i->!i.getCode().equals(areaCode.getCode())).collect(Collectors.toList());
        returnVO.setAreaCodes(list);
        return ResponseEntity.ok(returnVO);
    }
}
